Process data

If I can avoid it, I prefer to separate code chunks for processing and plotting.

#Process and summarise data-----------------------------------------------------------------
EcoMon_sum <- LME_nutrients_spatial %>% 
  filter(Value != -999, EPU %in% c("GB","GOM","SS")) %>% 
  
  #Create sample depth bins---------------------------------------------------------------------
  mutate(Value = as.numeric(Value),
         Depth_station = as.numeric(Depth_station),
         Depth_sampling = as.numeric(Depth_sampling)) %>% 
  mutate(bot_dif = Depth_station-Depth_sampling) %>% 
  mutate(surf_bot = ifelse(bot_dif <= 10, "Bottom",
                           ifelse(bot_dif > 10 & Depth_sampling <= 5, "Surface", "mid-water"))) %>%
  
  #Create seasonal bins-------------------------------------------------------------------------
  filter(month(Time) %in% c(10,11,5,6)) %>% 
  mutate(season = ifelse(month(Time) == 11 | month(Time) == 10, "Fall","Spring")) %>% 
  
  #Summarise------------------------------------------------------------------------------------
  group_by(EPU,
           season,
           surf_bot,
           Var,
           Year = year(Time)) %>% 
  dplyr::summarise(Value = mean(Value, na.rm = T)) %>% 
  as.data.frame()

Plot data

Plots can be embedded easily.

EcoMon_sum %>% 
  filter(!Var %in% c("BTLNBR","CASTNO","Cruise_ID","EXPOCODE","STNNBR")) %>% 
  ggplot() +
  geom_line(aes(x = Year, y = Value, color = surf_bot)) +
  guides(color = guide_legend("Sampling\n depth")) +
  facet_grid(Var~EPU + season, scales = "free") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text.y = element_text(size = 6)) 
Summarized nutrient and oceanographic data from the NEFSC EcoMon surveys in New Enlgand Ecological Production Units (EPUs).

Summarized nutrient and oceanographic data from the NEFSC EcoMon surveys in New Enlgand Ecological Production Units (EPUs).

Tables

So can tables!

Variable definitions for the LME_nutrients_spatial.rdata file.
Variable Names Units
Cruise identifier EXPOCODE
Cruise identifier Cruise_ID
Station number STNNBR
CTD cast number CASTNO
Sample bottle number BTLNBR
Sample date Date_UTC MM/DD/YYYY
Sample time Time_UTC hh:mm
Latitude Latitude decimal degrees
Longitude Longitude decimal degrees
Depth of station Depth_station m
Depth of sample Depth_sampling m
Water pressure CTDPRS decibars
Water temperature CTDTEMP °C
Water salinity CTDSAL PSS-78
Potential density at surface pressure Sigma.Theta kg m-3
Dissolved oxygen CTDOXY mg L-1
Silicic acid concentration SILCAT \(\mu\)M
Total nitrate and nitrite concentration NITRIT+NITRAT \(\mu\)M
Ammonia concentration AMMMONIA \(\mu\)M
Phosphate concentration PHSPHT \(\mu\)M
Dissolved oxygen CTDOXYMOL \(\mu\)mol kg-1

HTML Widgets

HTML widgets use R functions as wrappers for JavaScript libraries. One of the benefits of knitting rmarkdown documents to HTML is that we’re not limited to static images, and can take advantage of these widgets. The interactive graphing library Plotly is a good example of an html widget with huge flexibility for data visualization.

Plotly

library(plotly)
# volcano is a numeric matrix that ships with R
p <- plot_ly(z = ~volcano) %>% add_surface()
p

plotly::ggplotly()

Figures produced with ggplot2 can be made interactive by passing ggplot objects to the ggplotly function. For example:

n_gg <- EcoMon_sum %>% 
  filter(Var %in% c("NITRIT.NITRAT")) %>% 
  ggplot() +
  geom_line(aes(x = Year, y = Value, color = surf_bot)) +
  facet_grid(EPU~season)

ggplotly(n_gg) %>% 
  layout(legend = list(orientation = 'h',
                       y = -0.1))

DT

DT is another HTML widget that interfaces with the JS DataTables library.

library(DT)
datatable(vars)

Tabbed output

Tabbed outputs can be added to save vertical space

The Code

cars_plot <- ggplot(data = mtcars) +
  geom_point(aes(x = wt, y = mpg)) +
  ggtitle("A plot")

The Figure